
package com.monlyu.action.user;

import java.util.Map;

import com.monlyu.action.BaseAction;
import com.monlyu.annotation.ActionAnno;
import com.monlyu.bo.UserBo;
import com.monlyu.config.AppArgs;
import com.monlyu.util.ActionUtil;
import com.monlyu.util.SecurityUtil;
import com.monlyu.util.WebOptUtil;
import com.monlyu.vo.User;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author monlyu
 * @since 2009-12-15
 */
public class LogonAction extends BaseAction {

    @Override
    @ActionAnno(needLogin = false)
    public String execute() throws Exception{
        String errMsg = "";
        if (user == null) {
            return ActionUtil.LOGON;
        }
        Map<String, Object> map = ActionContext.getContext().getSession();
        boolean open = true;
        if (WebOptUtil.APP_ARGS.getAuthCode().isActive() && !WebOptUtil.APP_ARGS.isDebug()) {
            open = map != null
                && map.get("AuthCode") != null
                && map.get("AuthCode").toString().toUpperCase().equals(authNo.toUpperCase());
        }
        if (open) {
            final User dbUser = userBo.findUser(user);
            if (dbUser == null) {
                errMsg = "对不起,数据库没有该用户";
            } else if (!dbUser.isEnable()) {
                errMsg = "对不起，此用户已禁用，不能登录";
            } else {
                if (SecurityUtil.md5(user.getPasswd()).equals(dbUser.getPasswd())) {
                    WebOptUtil.remember(dbUser);
                    // WebOptUtil.rememberTmpUser(userBo.getRemoteUser(dbUser));
                } else {
                    errMsg = "对不起,用户密码不正确";
                }
            }
        } else {
            errMsg = "验证码错误";
        }
        return ajax(errMsg);
    }

    private User user;
    private UserBo userBo;
    private String authNo;

    public void setAuthNo(String authNo){
        this.authNo = authNo;
    }

    public void setUserBo(UserBo userBo){
        this.userBo = userBo;
    }

    public User getUser(){
        return user;
    }

    public void setUser(User user){
        this.user = user;
    }

    public static void main(String[] args){
        System.out.println(SecurityUtil.md5("admin"));
    }
}
